$m(function (args) {
    //获取详情页面头部信息
    var datas = args.data;
    //服务类型
    var ctype = datas.CTYPE;
    //发布人
    var creator = datas.CREATOR;
    if (creator.length > 6) {
        creator = creator.substr(0, 7) + "...";
    }
    //服务名称
    var serviceName = datas.SERVENAME;
    //空间名称
    var workspaceName = datas.WORKSPACENAME;
    //空间名称
    var themeName = datas.APPLICATION;
    //任务发布时间
    var creattime = DateUtils.formatDate(datas.CREATETIME, 'yyyy-MM-dd hh:mm:ss');

    //服务名称dom元素
    var serviceNameDom = $("#ca-name");
    //发布人dom元素
    var taskCreatorDom = $("#task-creator");
    //空间名称dom元素
    var workspaceDom = $("#belong_workspace");
    //应用名称dom元素
    var themeDom = $("#belong_theme");
    //任务发布时间dom元素
    var createTimeDom = $("#task-createTime")
    //服务状态dom元素
    var serviceStatus = $(".task-runtStatus");
    //页面容器
    var $container = $(".task-right-bot-container");

    /**
     * 初始化头部页面
     */
    var initTop = function () {
        //服务名称
        serviceNameDom.text(serviceName).attr("title", serviceName);
        //管道流程增加PL标识
        if (datas.FLOWTYPE == "plflow") {
            var plImg = $("<img/>").addClass("tempo-cgfl-details-sync-service-pl-image");
            plImg.attr("src", contextPath + "/css/images/pl_icon.png");
            var serviceNameWidth = serviceNameDom.width();
            if (serviceNameWidth >= 420) {
                serviceNameDom.after(plImg);
                plImg.css("margin-left", "-7px").css("margin-top", "5px").css("position", "absolute");
            } else {
                serviceNameDom.append(plImg);
                plImg.css("margin-left", "5px").css("margin-top", "5px").css("position", "absolute");
            }
        }
        //发布人
        taskCreatorDom.text(creator).attr("title", datas.CREATOR);
        //空间名称
        workspaceDom.text(workspaceName).attr("title", workspaceName);
        //应用名称
        themeDom.text(themeName).attr("title", themeName);
        //发布时间
        createTimeDom.html(creattime);
        //服务状态增加ID标识
        serviceStatus.attr("id", datas.SERVICEID);

        //初始化不同服务头部页面
        if (ctype == 'asyn') {
            $(".tempo-label-task-runtStatus").html("");
            serviceStatus.html("");
        } else if (ctype == 'sync' || ctype == 'stream') {
            $(".tempo-label-task-runtStatus").html("服务状态：");
            //成果管理点击服务名称进入服务详情页面，若服务状态为未启动或已启动，不进行定时器的刷新
            if (datas.ASY_SERVIECESTATE == "RUNNING" || datas.LAUNCH_STATE == "RUNNING") {
                serviceStatus.html("已启动");
            } else if (datas.ASY_SERVIECESTATE == "STARTING" || datas.LAUNCH_STATE == "STARTING") {
                serviceStatus.html("启动中");
                //定时器定时刷新服务状态
                updateStatusLoop(datas.SERVICEID, null, ctype);
            } else {
                serviceStatus.html("未启动");
            }
        }
    }

    /**
     * 初始化页面tab点击事件
     */
    var initBindEvent = function () {
        //默认选中流程概览页面
        $m.loadPage(contextPath + "/api/ai/app/view/overview?id=" + datas.FLOWID + "&&t=" + TempoUtils.uuid() + "&&serviceId=" + datas.SERVICEID, $container);

        //流程概览点击事件
        $("#overview").click(function () {
            changeTab($(this));
            $m.loadPage(contextPath + "/api/ai/app/view/overview?id=" + datas.FLOWID + "&&t=" + TempoUtils.uuid() + "&&serviceId=" + datas.SERVICEID, $container);
        });

        //服务配置点击事件
        $("#taskconfiguration").click(function () {
            changeTab($(this));
            $m.loadPage(contextPath + "/api/ai/app/view/configuration?id=" + datas.FLOWID + "&&t=" + TempoUtils.uuid() + "&&serviceId=" + datas.SERVICEID, $container);
        });

        //服务监控点击事件
        $("#taskmonitor").click(function () {
            changeTab($(this));
            $m.loadPage(contextPath + "/api/ai/app/view/monitor?id=" + datas.FLOWID + "&&t=" + TempoUtils.uuid() + "&&serviceId=" + datas.SERVICEID, $container);
        });

        //服务审计点击事件
        $("#taskaudit").click(function () {
            changeTab($(this));
            $m.loadPage(contextPath + "/api/ai/app/view/audit?id=" + datas.FLOWID + "&&t=" + TempoUtils.uuid() + "&&serviceId=" + datas.SERVICEID, $container);
        });
    }

    /**
     * 切换页签
     * @param that
     */
    var changeTab = function (that) {
        //将所有tab底边样式去除
        $(".tempo-head-li").css("border-bottom", "");
        //设置底边样式
        that.css("border-bottom", "2px solid #1F73B6");
        //清空容器
        $container.empty();
    }

    /**
     * 请求服务状态
     * @param fn
     * @param uid
     */
    var isAlive = function (fn, uid) {
        var id = uid || row.id
        $.postForm({
            url: contextPath + '/api/ai/app/model/status/' + id,
            success: function (rp) {
                fn(rp.data == 'STARTING' || rp.data == 'RUNNING', rp.data);
            }
        });
    }

    /**
     * 定时器定时刷新成果管理服务状态
     * @param id
     * @param updateFn
     * @param type
     */
    var updateStatusLoop = function (id, updateFn, type) {
        if (type != 'sync' && type != 'stream') {
            window.manageLoopUpdateTimer = -1;
            return;
        }
        /**
         * 成果管理点击服务名称进入服务详情页面，若服务状态为启动中，进行定时器刷新获取服务状态，若
         * @type {number}
         */
        var timer = setTimeout(function () {
            var fn = arguments.callee;
            isAlive(function (d, status) {
                if (window.manageLoopUpdateTimer != timer) {
                    return;
                }
                if (status == 'STARTING') {
                    serviceStatusUpdate(TempoUtils.locale("aiview.serviceStarting"), id);
                    timer = setTimeout(fn, 1000);
                    window.manageLoopUpdateTimer = timer;
                } else if (status == 'RUNNING') {
                    serviceStatusUpdate(TempoUtils.locale("aiview.serviceHasStart"), id);
                    //已启动状态服务配置页面按钮变为停止并去除禁用状态
                    $container.find('a.start-button').text(TempoUtils.locale("aiview.serviceStop"));
                    $container.find('a.start-button').removeClass("ui-state-disabled").attr("disabled", false);
                    if (updateFn) updateFn();
                } else {
                    serviceStatusUpdate(TempoUtils.locale("aiview.serviceUnStart"), id);
                    //未启动状态服务配置页面按钮变为启动并去除禁用状态
                    $container.find('a.start-button').text(TempoUtils.locale("aiview.serviceStart"));
                    $container.find('a.start-button').removeClass("ui-state-disabled").attr("disabled", false);
                    if (updateFn) updateFn();
                }
            }, id);
        });
        //成果管理页面服务状态刷新定时器
        window.manageLoopUpdateTimer = timer;
    }

    /**
     * 更新服务状态
     * @param status
     * @param id
     */
    var serviceStatusUpdate = function (status, id) {
        $('span[id="' + id + '"]').text(status);
    }

    /**
     * 初始化页面
     */
    var init = function () {
        //初始化头部页面
        initTop();
        //初始化页面tab点击事件
        initBindEvent();
    }

    init();

})